package icu.bughub.app.app.ui.components

import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.pager.PagerState
import androidx.compose.foundation.pager.VerticalPager
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.DisposableEffect
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import icu.bughub.app.app.viewmodel.MainViewModel
import kotlinx.coroutines.launch
import java.util.Timer
import java.util.TimerTask

/**
 *  通知公告
 * @param viewModel
 * @receiver Modifier
 */
@Composable
fun NotificationContent(viewModel: MainViewModel) {

    val coroutineScope = rememberCoroutineScope()


    //虚拟页数
    var virtualCount = Int.MAX_VALUE / 2
    //实际页数
    var actualCount = viewModel.notifications.size
    //初始图片下标
    var initialIndex = virtualCount / 2

    val pagerState = PagerState {
        virtualCount
    }


    //定时器
    DisposableEffect(Unit) {
        val timer = Timer()
        timer.schedule(object : TimerTask() {
            override fun run() {
                coroutineScope.launch {
                    pagerState.animateScrollToPage(pagerState.currentPage + 1)
                }
            }
        }, 3000, 3000)

        onDispose {
            timer.cancel()
        }

    }

    Row(
        modifier = Modifier
            .padding(horizontal = 8.dp, vertical = 8.dp)
            .clip(RoundedCornerShape(8.dp))
            .background(Color(0x22149EE7))
            .height(45.dp)
            .then(Modifier.padding(horizontal = 8.dp)),
        verticalAlignment = Alignment.CenterVertically,
        horizontalArrangement = Arrangement.SpaceBetween
    ) {
        Text(text = "最新活动", color = Color(0x66149EE7), fontSize = 14.sp)
        Spacer(modifier = Modifier.width(10.dp))
        VerticalPager(
            state = pagerState,
            modifier = Modifier.weight(1f),
            horizontalAlignment = Alignment.Start
        ) { index ->
            var actualIndex = (index - initialIndex).floorMod(actualCount)
            Text(
                text = viewModel.notifications[actualIndex],
                color = Color(0xFF333333),
                modifier = Modifier
                    .padding(vertical = 8.dp)
                    .padding(horizontal = 8.dp),
                //.fillMaxWidth(),
                fontSize = 12.sp,
                maxLines = 1,
                overflow = TextOverflow.Ellipsis,

                )
        }
        Spacer(modifier = Modifier.width(10.dp))
        Text(text = "更多", color = Color(0xFF666666), fontSize = 12.sp, maxLines = 1)
    }
}

